#! /usr/bin/env python
# -*- coding: UTF-8 -*-
"""
Created by jianbing on 2017-09-08
"""
import abc
import copy

from report.lib import template, option
from report.lib.util import CallHelper, register, dumps_option


class BaseChart(abc.ABC):
    _chart_count = 1

    def __init__(self, title, subtitle, width, height):
        self._title = title
        self._subtitle = subtitle
        self._width = width
        self._height = height
        self._legend_data = []
        self._x_axis_data = None
        self._series = []

        self._chart_id = "Chart_{}".format(self._chart_count)
        BaseChart._chart_count += 1
        self._op = None

        # 设置
        self._is_show_legend = True
        self._is_show_title = True

        CallHelper.sign(self)

    def render(self):
        CallHelper.check_require_call(self)
        self._make_option()
        self._make_setting()
        return template.t_chart.format(chart_id=self._chart_id,
                                       op=dumps_option(self._op),
                                       width=self._width,
                                       height=self._height
                                       )

    def _make_option(self):
        self._op['title']['text'] = self._title
        self._op['title']['subtext'] = self._subtitle
        self._op['legend']['data'] = self._legend_data
        self._op['series'] = self._series

    def _make_setting(self):
        if not self._is_show_legend:
            self._op['legend']['data'] = None

        if not self._is_show_title:
            self._op['title']['text'] = None
            self._op['title']['subtext'] = None

    def set_show_legend(self, show: bool):
        """设置是否展示图例

        :param show:
        :return:
        """
        self._is_show_legend = show

    def set_show_title(self, show: bool):
        """设置是否显示标题

        :param show:
        :return:
        """
        self._is_show_title = show


class BarChart(BaseChart):
    def __init__(self, title, subtitle="", width=None, height=None):
        super().__init__(title, subtitle, width, height)

        self._op = copy.deepcopy(option.op_bar_chart)

    @register(1)
    def add_x_axis(self, data):
        self._x_axis_data = data

    @register()
    def add_series(self, series_name: str, series_data: list):
        self._legend_data.append(series_name)
        self._series.append({
            "name": series_name,
            "type": 'bar',
            "data": series_data,
            "label": {
                "normal": {
                    "show": True,
                    "position": 'inside'
                }
            }
        })

    def _make_option(self):
        super()._make_option()
        if not self._width:
            self._width = len(self._x_axis_data) * len(self._legend_data) * 100
        if not self._height:
            self._height = 400

        self._op['xAxis']['data'] = self._x_axis_data


class LineChart(BaseChart):
    def __init__(self, title, subtitle="", width=None, height=None):
        super().__init__(title, subtitle, width, height)

        self._op = copy.deepcopy(option.op_line_chart)

    @register(1)
    def add_x_axis(self, data):
        self._x_axis_data = data

    @register()
    def add_series(self, series_name: str, series_data: list):
        self._legend_data.append(series_name)
        self._series.append({
            "name": series_name,
            "type": 'line',
            "data": series_data,
            "label": {
                "normal": {
                    "show": True,
                    "position": 'top'
                }
            },
        })

    def _make_option(self):
        super()._make_option()
        if not self._width:
            self._width = len(self._x_axis_data) * 100
        if not self._height:
            self._height = 400

        self._op['xAxis']['data'] = self._x_axis_data


class PieChart(BaseChart):
    def __init__(self, title, subtitle="", width=500, height=500):
        super().__init__(title, subtitle, width, height)

        self._op = copy.deepcopy(option.op_pie_chart)

    @register(1)
    def add_series(self, series_name: list, series_data: list):
        self._legend_data = series_name
        data = []
        for name, value in zip(series_name, series_data):
            data.append({"value": value, "name": name})
        self._series.append({
            "name": '访问来源',
            "type": 'pie',
            "radius": '65%',
            "center": ['50%', '50%'],
            "data": data,
            "itemStyle": {
                "emphasis": {
                    "shadowBlur": 10,
                    "shadowOffsetX": 0,
                    "shadowColor": 'rgba(0, 0, 0, 0.5)'
                }
            }
        })

    def _make_option(self):
        super()._make_option()
